package com.juku2024.juku.adapter

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.chad.library.adapter.base.BaseDelegateMultiAdapter
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.juku2024.juku.R
import com.juku2024.juku.VideoDetailActivity
import com.juku2024.juku.WatchingActivity
import com.juku2024.juku.model.CarouselModel
import com.juku2024.juku.model.HomeAdapterItem
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.ImageUtils
import com.juku2024.juku.viewmodel.MainViewModel
import com.juku2024.juku.widget.LoadingView
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.bannerview.constants.PageStyle
import com.zhpan.indicator.IndicatorView
import com.zhpan.indicator.enums.IndicatorSlideMode
import com.zhpan.indicator.enums.IndicatorStyle

class HomeTabAdapter(model: ArrayList<HomeAdapterItem>?) :
    BaseDelegateMultiAdapter<HomeAdapterItem, BaseViewHolder>(model), LoadMoreModule {

    init {
        setMultiTypeDelegate(object : BaseMultiTypeDelegate<HomeAdapterItem>() {
            override fun getItemType(data: List<HomeAdapterItem>, position: Int): Int {
                return data[position].adapterType
            }
        })
        // 	模版(1:竖版 2:横板 3:竖版(电影) 4:横版(电影) 5:广告)
        getMultiTypeDelegate()?.apply {
            addItemType(
                MainViewModel.TYPE_BANNER,
                R.layout.item_home_banner_new
            )
            addItemType(
                MainViewModel.TYPE_TITLE,
                R.layout.item_home_tab_title
            )
            addItemType(
                MainViewModel.TYPE_MORE,
                R.layout.item_home_tab_more
            )
            addItemType(
                MainViewModel.TYPE_GRID,
                R.layout.item_home_tab_grid2
            )
            addItemType(
                MainViewModel.TYPE_WATCHING,
                R.layout.item_home_watching1
            )
        }

        addChildClickViewIds(R.id.titleNext, R.id.tv_home_more, R.id.homeGrid2)
    }


    override fun convert(holder: BaseViewHolder, item: HomeAdapterItem) {
        // 	模版(1:竖版 2:横板 3:竖版(电影) 4:横版(电影) 5:广告)
        when (holder.itemViewType) {
            MainViewModel.TYPE_TITLE -> {
                holder.setText(R.id.tv_home_title, item.name)
            }
            MainViewModel.TYPE_GRID -> {
                val iv = holder.getView<ImageView>(R.id.iv_home)
                ImageUtils.loadImageV(iv, item.pic)
                val colorId = if (item.isMovie == true)
                    R.color.cFF6D2F
                else
                    R.color.cFFFFFF
                holder.setText(R.id.tv_update, item.content)
                    .setText(R.id.tv_title, item.name)
                    .setTextColor(R.id.tv_update, AppUtils.getColor(colorId))
            }

            MainViewModel.TYPE_BANNER -> {
                val banner = holder.getView<BannerViewPager<CarouselModel>>(R.id.tabBanner)
                val indicatorView = holder.getView<IndicatorView>(R.id.indicator_view)
                val activity = AppUtils.findAppCompatActivity(context)
                banner.registerLifecycleObserver(activity?.lifecycle)
                    .setAdapter(TopHomeBannerAdapter())
                    .setPageStyle(PageStyle.MULTI_PAGE_SCALE)
                    .setPageMargin(20)
                    .setRoundCorner(12)
                    .setRevealWidth(20, 20)
                    .setIndicatorVisibility(View.GONE)
                    .setScrollDuration(400)
                    .setOnPageClickListener { _, position ->
                        // 跳转详情
                        val bannerItem = item.carousel?.get(position)
                        if (TextUtils.isEmpty(bannerItem?.url)) {
                            VideoDetailActivity.toVideoDetail(context, bannerItem?.id, bannerItem?.name)
                        } else {
                            AppUtils.toWeb(bannerItem?.url!!)
                        }
                    }
                    .registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                            indicatorView.onPageScrolled(position, positionOffset, positionOffsetPixels)
                        }

                        override fun onPageSelected(position: Int) {
                            super.onPageSelected(position)
                            indicatorView.onPageSelected(position)
                        }
                    })
                    .create(item.carousel)


                indicatorView.apply {
                    setSliderColor(AppUtils.getColor(R.color.c9B9B9B), AppUtils.getColor(R.color.cFF6D2F))
                    setSliderWidth(resources.getDimension(R.dimen.d_8), resources.getDimension(R.dimen.d_15))
                    setSliderHeight(resources.getDimension(R.dimen.d_4))
                    setSlideMode(IndicatorSlideMode.SCALE)
                    setIndicatorStyle(IndicatorStyle.ROUND_RECT)
                    setPageSize(item.carousel?.size?:0)
                    notifyDataChanged()
                }
            }

            MainViewModel.TYPE_WATCHING -> {
//                val tvWatching = holder.getView<TextView>(R.id.tvWatching)
//                if (item.watching.isNullOrEmpty()) {
//                    tvWatching.visibility = View.GONE
//                    return
//                }
//                tvWatching.visibility = View.VISIBLE
//                tvWatching.text = if (false == item.isNewEpisode) {
//                    "追剧片单暂无更新！"
//                } else {
//                    "有新剧更新了！去查看 ->"
//                }
                val loadingView = holder.getView<LoadingView>(R.id.watchLoading)
                if (item.watching.isNullOrEmpty()) {
                    loadingView.showLoading()
                    return
                }
                loadingView.hideLoading()
                val watchingRecycler = holder.getView<RecyclerView>(R.id.watchRecycler)
                watchingRecycler.setHasFixedSize(true)
                watchingRecycler.layoutManager = LinearLayoutManager(context,  LinearLayoutManager.HORIZONTAL, false)
                val adapter = WatchListAdapter(item.watching!!)
                watchingRecycler.adapter = adapter
                adapter.setOnItemClickListener { _, _, position ->
                    VideoDetailActivity.toVideoDetail(context, adapter.data[position].id, adapter.data[position].name)
                }

                val title = holder.getView<View>(R.id.titleNext)
                title.setOnClickListener {
                    val intent = Intent(context, WatchingActivity::class.java)
                    intent.putParcelableArrayListExtra("watching", item.watching)
                    context.startActivity(intent)
                }
            }
        }
    }

}